package com.samebuilding.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.samebuilding.dao.ActivityMemberDAO;
import com.samebuilding.domain.ActivityMember;

public class ActivityMemberDAOImpl extends GenericDAOImpl<ActivityMember, Integer> implements
		ActivityMemberDAO {

	private static final String FIND_BY_ACTIVITYID = "from ActivityMember where activityId = ?";
	private static final String FIND_COUNT_BY_ACTIVITYID = "select count(memberId) from ActivityMember where activityId = ?";
	private static final String DELETE_BY_ACTIVITYID = "delete from ActivityMember where activityId = ?";
	private static final String DELETE_BY_ACTIVITYID_AND_UID = "delete from ActivityMember where activityId = ? and memberUid = ?";
	private static final String IS_CONTAIN_RECORD = "from ActivityMember where activityId = ? and memberUid = ?";
	
	@SuppressWarnings("unchecked")
	public List<ActivityMember> findActivityMemberByActivityId(final int activityId, final int startRecord,final int maxRecord) {
		List<ActivityMember> list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(FIND_BY_ACTIVITYID);
				query.setInteger(0, activityId);
				query.setFirstResult(startRecord);
				query.setMaxResults(maxRecord);
				List<ActivityMember> list = query.list();
				return list;
			}
		});
		return list;
	}

	public int findCountActivityMemberByActivityId(final int activityId) {
		long count = (Long)this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(FIND_COUNT_BY_ACTIVITYID);
				query.setInteger(0, activityId);

				return query.uniqueResult();
			}
		});
		
		return (int)count;
	}

	public void deleteActivityMemberByActivityId(final int activityId) {
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				Query query = s.createQuery(DELETE_BY_ACTIVITYID);
				query.setInteger(0, activityId);
				query.executeUpdate();
				return null;
			}
		});
	}

	public void deleteByActivityIdAndUid(final int activityId, final int uid) {
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				Query query = s.createQuery(DELETE_BY_ACTIVITYID_AND_UID);
				query.setInteger(0, activityId);
				query.setInteger(1, uid);
				query.executeUpdate();
				return null;
			}
		});
		
	}

	
	public boolean isContainRecord(final int activityId, final int uid) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(IS_CONTAIN_RECORD);
				query.setInteger(0, activityId);
				query.setInteger(1, uid);
				return query.list();
			}
		});
		
		if(list == null || list.isEmpty()) {
			return false;
		} else {
			return true;
		}
	}

}
